package com.sayimo.maker.base.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.base.service.BasePhotoService;
import com.sayimo.maker.base.service.UploadFileService;
import com.sayimo.maker.base.vo.BasePhoto;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.commons.utils.Base64Utils;
import com.sayimo.maker.commons.utils.JSONUtil;
import com.sayimo.maker.commons.utils.PropertiesUtils;
import com.sayimo.uploadfile.util.UploadFileInfo;
import com.sayimo.uploadfile.util.UploadFileResult;
import com.sayimo.uploadfile.util.UploadFileUtil;

/**
 * @Copyright (c) 2015 广东省尚一互联网有限公司
 * @Project: schoolmallapi
 * @ClassName: UploadFileServiceImpl
 * @Description:
 * @Author: chenpy
 * @Date: 日期：2015年12月29日 时间：上午10:16:32
 * @Version 1.0.0
 */
@Service(value = "uploadFileService")
public class UploadFileServiceImpl implements UploadFileService {

	private static Log logger = LogFactory.getLog(UploadFileServiceImpl.class);

	@Resource(name="basePhotoServiceImpl")
	private BasePhotoService basePhotoServiceImpl;


	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Map uploadFile(File[] files, String[] fileNames, String filePath, String imagePath) throws ServiceException {
		if (filePath == null && imagePath == null) {
			filePath = PropertiesUtils.getValue("FILE_SAVE_PATH") + "/other";
			imagePath = PropertiesUtils.getValue("OTHERIMGPATH");
		}
		Map resultMap = new HashMap();
		UploadFileResult uploadResult = UploadFileUtil.upload(PropertiesUtils.getValue("UPLOAD_SERVER"), files,
				fileNames, filePath, imagePath);
		UploadFileInfo[] fileInfos = uploadResult.getFiles();
		JSONArray photoArray = new JSONArray();
		if (!ArrayUtils.isEmpty(fileInfos)) {
			for (int i = 0; i < fileInfos.length; i++) {
				UploadFileInfo fileInfo = fileInfos[i];
				BasePhoto basePhoto = new BasePhoto();
				basePhoto.setPhotoPath(fileInfo.getFileUrl() + "/" + fileInfo.getProFileName());
				basePhoto.setShowName(fileInfo.getUpLoadfileName());
				basePhoto.setPhotoName(fileInfo.getProFileName());
				basePhoto.setPhotoType(fileInfo.getFileType());
				JSONObject photo = new JSONObject();
				photo.put("fileUrl", basePhoto.getPhotoPath());
				photoArray.put(photo);
			}
		}
		resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
		resultMap.put(Constant.DATA, JSONUtil.readValueToBeanArray(photoArray.toString(), Map.class));
		return resultMap;
	}
	
	public Map writeFilePHP(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		String fileStr = request.getParameter("fileStr");
		String fileName = request.getParameter("fileName");
		String filePath = request.getParameter("filePath");
		String imagePath = request.getParameter("imagePath");
		if (filePath == null && imagePath == null) {
			filePath = PropertiesUtils.getValue("FILE_SAVE_PATH") + "/other";
			imagePath = PropertiesUtils.getValue("OTHERIMGPATH");
		}
		try {
			UploadFileResult uploadResult = Base64Utils.uploadByBase64(
					PropertiesUtils.getValue("UPLOAD_SERVER_BASE64"), fileStr, fileName, filePath, imagePath);
			logger.info("UPLOAD_SERVER_BASE64");
			JSONArray photoArray = new JSONArray();
			UploadFileInfo fileInfo = uploadResult.getFiles()[0];
			if (fileInfo != null && !fileInfo.equals("")) {
				BasePhoto basePhoto = new BasePhoto();
				basePhoto.setPhotoPath(fileInfo.getFileUrl() + "/" + fileInfo.getProFileName());
				basePhoto.setShowName(fileInfo.getUpLoadfileName());
				basePhoto.setPhotoName(fileInfo.getProFileName());
				basePhoto.setPhotoType(fileInfo.getFileType());
				JSONObject photo = new JSONObject();
				photo.put("photoUrl", basePhoto.getPhotoPath());
				photoArray.put(photo);
			}
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.DATA, JSONUtil.readValueToBeanArray(photoArray.toString(), Map.class));
			return resultMap;
		} catch (Exception e) {
			logger.info(e);
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
			return resultMap;
		}
	}

	public Map writeFile(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		String fileStr = request.getParameter("fileStr");
		String fileName = request.getParameter("fileName");
		String filePath = request.getParameter("filePath");
		String imagePath = request.getParameter("imagePath");
		String type = request.getParameter("type");
		if (filePath == null && imagePath == null) {
			filePath = PropertiesUtils.getValue("FILE_SAVE_PATH") + "/other";
			imagePath = PropertiesUtils.getValue("OTHERIMGPATH");
		}
		try {
			UploadFileResult uploadResult = Base64Utils.uploadByBase64(
					PropertiesUtils.getValue("UPLOAD_SERVER_BASE64"), fileStr, fileName, filePath, imagePath);
			logger.info("UPLOAD_SERVER_BASE64");
			JSONArray photoArray = new JSONArray();
			UploadFileInfo fileInfo = uploadResult.getFiles()[0];
			if (fileInfo != null && !fileInfo.equals("")) {
//				if (type != null && !type.equals("") && type.equals("1")) {
//					GoodsPhoto goodsPhoto = new GoodsPhoto();
//					goodsPhoto.setPhotoPath(fileInfo.getFileUrl() + "/" + fileInfo.getProFileName());
//					goodsPhoto.setPhotoName(fileInfo.getProFileName());
//					JSONObject photo = new JSONObject();
//					goodsPhotoDao.insertGoodsPhoto(goodsPhoto);
//					photo.put("id", goodsPhoto.getId());
//					photo.put("photoUrl", goodsPhoto.getPhotoPath());
//					photoArray.put(photo);
//				} else 
					if (type == null||type != null && !type.equals("") && type.equals("2")) {
					BasePhoto basePhoto = new BasePhoto();
					basePhoto.setPhotoPath(fileInfo.getFileUrl() + "/" + fileInfo.getProFileName());
					basePhoto.setShowName(fileInfo.getUpLoadfileName());
					basePhoto.setPhotoName(fileInfo.getProFileName());
					basePhoto.setPhotoType(fileInfo.getFileType());
					JSONObject photo = new JSONObject();
					basePhotoServiceImpl.insertBasePhoto(basePhoto);
					photo.put("id", basePhoto.getId());
					photo.put("photoUrl", basePhoto.getPhotoPath());
					photoArray.put(photo);
				}
			}
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.DATA, JSONUtil.readValueToBeanArray(photoArray.toString(), Map.class));
			return resultMap;
		} catch (Exception e) {
			logger.info(e);
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
			return resultMap;
		}
	}
}
